package com.xm.dao.reception_desk;

import com.xm.entity.CommodityParameter;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品参数
 * @author 康文程
 * @date 2024-1-23
 */
public class CommodityParameterDao extends BaseUtil {
    /**
     * 根据商品ID查询商品参数列表
     *
     * @param commodityId 商品ID
     * @return List<CommodityParameter> 商品参数列表
     */
    public List<CommodityParameter> getCommodityParametersByCommodityId(Integer commodityId) {
        @Language("SQL")
        String sql = "SELECT parameter_id, parameter_images, parameter_title, parameter_context, commodity_id " +
                "FROM commodity_parameter WHERE commodity_id = ?";
        ResultSet rs = null;
        List<CommodityParameter> parameterList = new ArrayList<>();
        try {
            rs = executeQuery(sql, commodityId);
            while (rs.next()) {
                CommodityParameter parameter = new CommodityParameter();
                parameter.setParameterId(rs.getInt("parameter_id"));
                parameter.setParameterImages(rs.getString("parameter_images"));
                parameter.setParameterTitle(rs.getString("parameter_title"));
                parameter.setParameterContext(rs.getString("parameter_context"));
                parameter.setCommodityId(rs.getInt("commodity_id"));
                parameterList.add(parameter);
            }
        } catch (SqlErrorException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(rs);
        }
        return parameterList;
    }
}
